package top.kaoshanji.v1ch13a;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import top.kaoshanji.v1ch13a.model.Account;
import top.kaoshanji.v1ch13a.repositories.AccountRepository;
import top.kaoshanji.v1ch13a.services.TransferService;

import java.math.BigDecimal;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/1 18:23
 */
@SpringBootTest
public class TransferServiceTests {

    @MockBean
    private AccountRepository accountRepository;

    @Autowired
    private TransferService transferService;

    /**
     * 代码清单test-v1ch13a-01：测试一下服务层和持久层
     */
    @Test
    void transferServiceTransferAmountTest() {
        Account sender = new Account();
        sender.setId(1);
        sender.setAmount(new BigDecimal(1000));

        Account receiver = new Account();
        receiver.setId(2);
        receiver.setAmount(new BigDecimal(1000));

        Mockito.when(accountRepository.findAccountById(1)).thenReturn(sender);
        Mockito.when(accountRepository.findAccountById(2)).thenReturn(receiver);

        transferService.transferMoney(1, 2, new BigDecimal(100));

        Mockito.verify(accountRepository).changeAmount(1, new BigDecimal(900));
        Mockito.verify(accountRepository).changeAmount(2, new BigDecimal(1100));
    }


}
